from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import os
from models import Obligation
from forms import ObligationForm

class ListHandler(webapp.RequestHandler):

    def get(self):
        context = {
            'title': 'Obligations',
            'instances': Obligation.all(),
            'root': 'obligations',
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/list.html')
        self.response.out.write(template.render(path, context))

class ItemHandler(webapp.RequestHandler):

    def get(self, key):
        obligation = Obligation.get(key)
        context = {
            'title': 'Obligation - %s' % (obligation.name, ),
            'instance': obligation,
            'root': 'obligations',
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/instance.html')
        self.response.out.write(template.render(path, context))

    def post(self, key):
        self.request.POST['committers']=self.request.POST.getall('committers')
        obligation = Obligation.get(key)
        data = ObligationForm(data=self.request.POST, instance=obligation)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.put()
            self.redirect('/calendar/')
        else:
            context = {
                'title': 'Create a new Obligation',
                'form': data,
                'action': '/obligations/edit/%s/' % (key, ),
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
            self.response.out.write(template.render(path, context))

class CreateHandler(webapp.RequestHandler):

    def get(self):
        context = {
            'title': 'Create a new Obligation',
            'form': ObligationForm(data=self.request.GET),
            'action': '/obligations/create/',
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
        self.response.out.write(template.render(path, context))

    def post(self):
        self.request.POST['committers']=self.request.POST.getall('committers')
        data = ObligationForm(data=self.request.POST)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.put()
            self.redirect('/calendar/')
        else:
            context = {
                'title': 'Create a new Obligation',
                'form': data,
                'action': '/obligations/create/',
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
            self.response.out.write(template.render(path, context))

class EditHandler(webapp.RequestHandler):

    def get(self, key):
        obligation = Obligation.get(key)
        context = {
            'title': 'Edit Obligation %s' % (obligation.name, ),
            'form': ObligationForm(instance=obligation),
            'action': '/obligations/%s/' % (obligation.key(), ),
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
        self.response.out.write(template.render(path, context))

class DeleteHandler(webapp.RequestHandler):

    def post(self, key):
        obligation = Obligation.get(key)
        obligation.delete()
        self.redirect('/calendar/')
        
def main():
    application = webapp.WSGIApplication([
        ('/obligations/', ListHandler),
        ('/obligations/create/', CreateHandler),
        (r'/obligations/edit/(.+)/', EditHandler),
        (r'/obligations/delete/(.+)/', DeleteHandler),
        (r'/obligations/(.+)/', ItemHandler),
        ], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
